/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public class TriggerTrackStyle : CommonTrackStyle<TriggerTrackStyle, TriggerEventItemStyle>
    {
        public static Color triggerColor = new Color(0.69f, 0.99f, 0.34f, 1f);

        protected override void InitStyle()
        {
            Top.style.borderLeftColor = triggerColor;
            TrackMenuList.style.borderLeftColor = triggerColor;
            Texture image = EditorGUIUtility.IconContent("d_CompositeCollider2D Icon").image;
            StyleBackground style = new StyleBackground(image as Texture2D);
            Icon.style.backgroundImage = style;
        }

        public override CommonChildTrackBase CreateChildTrack()
        {
            return new CommonChildTrack();
        }

        public class CommonChildTrack : CommonChildTrackBase
        {
            public override void InitEvent()
            {
                if (_data == null || _data.events == null) return;

                for (int i = 0; i < _data.events.Count; i++)
                {
                    var e = _data.events[i] as TriggerEvent;
                    var eventItemStyle = new TriggerEventItemStyle();
                    eventItemStyle.Init(this, e);
                    _eventItemStyleList.Add(eventItemStyle);
                }
            }

            protected override void InitTrackMouseMene(VisualElement trackParent)
            {
                // // 创建右键菜单
                var menu = new ContextualMenuManipulator(evt =>
                {
                    evt.menu.AppendAction("添加立方体触发事件", CreateBoxTrigger, addState);
                    evt.menu.AppendAction("添加球形触发事件", CreateSphereTrigger, addState);
                    evt.menu.AppendAction("添加扇形触发事件", CreateFanTrigger, addState);
                    addState = DropdownMenuAction.Status.Normal;
                });

                // 将右键菜单添加到 trackRoot 上
                trackParent.AddManipulator(menu);
            }

            private void CreateFanTrigger(DropdownMenuAction obj)
            {
                var localPositon = trackRoot.WorldToLocal(obj.eventInfo.mousePosition);
                int selectFrameIndex = ActionEditor.Instance.GetFrameIndexByPos(localPositon.x);

                var triggerEvent = DataBase.New<SectorTriggerEvent>();
                triggerEvent.durationIndex = 5;
                triggerEvent.startIndex = selectFrameIndex;
                var eventItemStyle = new TriggerEventItemStyle();
                eventItemStyle.Init(this, triggerEvent);
                TUndo.RegisterCreatedObjectUndo(triggerEvent, "CreateFanTrigger");
                TUndo.RecordObject(_data, "CreateFanTrigger");
                _data.AddEvent(triggerEvent);
                AssetDatabase.AddObjectToAsset(triggerEvent, _data);
                EditorUtility.SetDirty(_data);
                AssetDatabase.SaveAssets();
                _eventItemStyleList.Add(eventItemStyle);
            }

            private void CreateSphereTrigger(DropdownMenuAction obj)
            {
                var localPositon = trackRoot.WorldToLocal(obj.eventInfo.mousePosition);
                int selectFrameIndex = ActionEditor.Instance.GetFrameIndexByPos(localPositon.x);

                var triggerEvent = DataBase.New<SphereTriggerEvent>();
                triggerEvent.startIndex = selectFrameIndex;
                triggerEvent.durationIndex = 5;
                var eventItemStyle = new TriggerEventItemStyle();
                eventItemStyle.Init(this, triggerEvent);
                TUndo.RegisterCreatedObjectUndo(triggerEvent, "CreateSphereTrigger");
                TUndo.RecordObject(_data, "CreateSphereTrigger");
                _data.AddEvent(triggerEvent);
                AssetDatabase.AddObjectToAsset(triggerEvent, _data);
                EditorUtility.SetDirty(_data);
                AssetDatabase.SaveAssets();
                _eventItemStyleList.Add(eventItemStyle);
            }

            private void CreateBoxTrigger(DropdownMenuAction obj)
            {
                var localPositon = trackRoot.WorldToLocal(obj.eventInfo.mousePosition);
                int selectFrameIndex = ActionEditor.Instance.GetFrameIndexByPos(localPositon.x);
                var triggerEvent = DataBase.New<BoxTriggerEvent>();
                triggerEvent.durationIndex = 5;
                triggerEvent.startIndex = selectFrameIndex;
                var eventItemStyle = new TriggerEventItemStyle();
                eventItemStyle.Init(this, triggerEvent);

                TUndo.RegisterCreatedObjectUndo(triggerEvent, "CreateBoxTrigger");
                TUndo.RecordObject(_data, "CreateBoxTrigger");
                _data.AddEvent(triggerEvent);
                AssetDatabase.AddObjectToAsset(triggerEvent, _data);
                EditorUtility.SetDirty(_data);
                AssetDatabase.SaveAssets();

                _eventItemStyleList.Add(eventItemStyle);
            }
        }
    }
}
